A fun and interactive memory game built with JavaScript.
The Simon Game is a fun and interactive memory game that challenges players to repeat an increasingly long sequence of colors and sounds. The game starts with one color, and with each level, a new color is added to the sequence. Players must correctly click the buttons in the same order as shown.
To implement this game, I used JavaScript to handle the game logic. The game listens for a keypress to start, and then a random color is selected and stored in an array. The player must then click the buttons in the correct order. If the player makes a mistake, the game plays a "wrong" sound and displays a Game Over message.
The code utilizes jQuery to handle user interactions and animations. Sounds are played using the JavaScript Audio object, and animations are applied when buttons are clicked. The game continues to increase in difficulty until the player makes an error, at which point they can restart and try again.
Building the Simon Game was a great learning experience. Here are some key takeaways:
Here are a few key code snippets from the project:
// Function to generate a random color sequence
var buttonColors = ["red", "blue", "green", "yellow"];
function nextSequence() {
userClickedPattern = [];
level++;
$("#level-title").text("Level " + level);
var randomNumber = Math.floor(Math.random() * 4);
var randomChosenColor = buttonColors[randomNumber];
gamePattern.push(randomChosenColor);
}
// Function to play a sound
function playSound(name) {
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}
You can download the full source code from my GitHub repository:
Download Simon Game Code.